#题目一#####
对象一=1
a1=1
.b=3
.b
#题目二#####
A<-c(1:9)
B<-seq(from=1,to=5,by=1)
D<-rep(0,5)
a=A[-3]
A[-c(7,8)]
A[A<5]
#sum;min;max;range;mean;var;order;sort;rank
#缺失值:NA,NAN;空值:NULL;无穷:lnf,-lnf
#因子tapply(a,b,mean)
N<-c(1:10)
N
sex<-c("female","female","female","1","female","male","male","male","1","male")
fsex<-factor(sex)##因子
Age<-c(12,18,19,16,20,20,18,38,19,18)
tapply(Age,fsex,mean)
#题目三#####
M<-c(3:12)
Q<-c(9:18)
data<-data.frame(fsex,M,N,Q)
data
#题目四####
num=sample(1:50,10,replace = TRUE)
num
j=0
for (i in num)
  if (i>10)
    j=j+1
j
##a的范围是1到9，若是取10，b会超出范围（冒泡排序）
for (a in 1:9)
  for(b in (a+1):10)
    if(num[a]<num[b])
    {temp<-num[b];num[b]<-num[a];num[a]<-temp;}##用到temp辅助交换
num
##直接插入排序
num1=sample(1:50,10,replace = TRUE)
num1
for (a in 2:10)
  {tem<-num1[a]
   b<-a-1
    while(num1[b]<tem&&0<b)##比有序区最小值小的位置不变，大的插入左边有序区，并从右到左依次确定位置
      {num1[b+1]<-num1[b];num1[b]<-tem;b<-b-1}}
num1
##折半插入排序
num2=sample(1:50,10,replace = TRUE)
num2
for (a in 2:10)
{b<-a-1
tem<-num2[a]
 high=1
 low=a-1
 while(low>=high)##与有序区中间位置比较，确定插入位置
   {mid=floor((low+high)/2)
    if(tem<num2[mid])
      high<-mid+1
    else
      low<-mid-1}
 while(b>=low+1)
 {num2[b+1]<-num2[b];b<-b-1;}##处理插入后剩余元素，后移
 num2[low+1]<-tem}##插入元素
num2 
##希尔排序
num3=sample(1:50,10,replace = TRUE)
num3
len<-length(num3)
a<-floor(len/2)##初始间隔
a
while(a>0){
  for (b in (a + 1):len){
    tem<-num3[b]
     c<-b-a
     while( c > 0 &&num3[c] < tem ){##插入排序
       num3[c+a]<-num3[c]
      c<-c-a
       }
        num3[c+a]<-tem}
  a<-floor(a/2)
  }
num3
